<%--
  Created by IntelliJ IDEA.
  Time: 14:59
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>
<html>
<head>
    <title>角色管理</title>
    <link rel="stylesheet" href="../static/layui/css/layui.css" media="all">
</head>
<body>
<div class="layui-row" id="editRole" style="display:none;">
    <div class="layui-col-md10">
        <form class="layui-form layui-from-pane" action="" style="margin-top:20px">
            <div class="layui-form-item">
                <label class="layui-form-label">角色名</label>
                <div class="layui-input-block">
                    <input type="text" name="editName" id="editName" required lay-verify="required" autocomplete="off"
                           placeholder="请输入角色名" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">标识</label>
                <div class="layui-input-block">
                    <input type="text" name="editSn" id="editSn" required lay-verify="required" autocomplete="off"
                           placeholder="请输入标识" class="layui-input">
                </div>
            </div>
        </form>
    </div>
</div>
<table class="layui-hide" id="article" lay-filter="article_bar"></table>
<script type="text/html" id="barArticle">
    <shiro:hasPermission name="role:update">
        <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit">
            <i class="layui-icon layui-icon-edit"></i>授权
        </a>
    </shiro:hasPermission>

    <shiro:hasPermission name="role:del">
        <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del"><i class="layui-icon layui-icon-delete"></i>删除</a>
    </shiro:hasPermission>
</script>
<script type="text/html" id="toolBar">
    <button class="layui-btn layui-btn-sm" data-type="add" lay-event="add"><i class="layui-icon"></i>添加</button>
</script>
</body>
</html>
<script src="../static/layui/layui.js"></script>
<script>
    layui.use(['laydate', 'laypage', 'layer', 'table', 'element','jquery'], function(){
        var laydate = layui.laydate //日期
            ,laypage = layui.laypage //分页
            ,layer = layui.layer //弹层
            ,table = layui.table //表格
            ,element = layui.element //元素操作
            ,$ = layui.jquery //jquery


        //监听Tab切换
        element.on('tab(article)', function(data){
            layer.tips('切换了 '+ data.index +'：'+ this.innerHTML, this, {
                tips: 1
            });
        });

        //执行一个 table 实例
        table.render({
            elem: '#article'
            ,height: 'full-30'
            ,url: '/role/findAll' //数据接口
            ,title: '用户表'
            ,limits:[20,30,50,100]
            ,limit : 20
            ,page: true //开启分页
            ,toolbar: '#toolBar' //开启工具栏，此处显示默认图标，可以自定义模板，详见文档
            ,cols: [[
                {type: 'checkbox', fixed: 'left'}//表头
                ,{field: 'id', title: 'ID', width:120, sort: true, fixed: 'left',align:'center'}
                ,{field: 'name', title: '名称', width:200, align:'center'}
                ,{field: 'sn', title: '标识', width:200, align:'center'}
                ,{fixed: 'right',title: '操作', width: 265, align:'center', toolbar: '#barArticle'}
            ]]
        });

        //监听头工具栏事件
        table.on('toolbar(article_bar)', function(obj){
            var checkStatus = table.checkStatus(obj.config.id)
                ,data = checkStatus.data; //获取选中的数据
            switch(obj.event){
                case 'add':
                    layer.open({
                        //layer提供了5种层类型。可传入的值有：0（信息框，默认）1（页面层）2（iframe层）3（加载层）4（tips层）
                        type: 1,
                        title: "添加角色信息",
                        content: $("#editRole")//引用的弹出层的页面层的方式加载修改界面表单
                        , btn: ['确定', '取消'],

                        btn1: function (index, layero) {
                            $.ajax({
                                url: '/role/add',
                                type: "post",
                                dataType: "json",
                                data: {
                                    name: $("#editName").val(),    //名称
                                    sn: $("#editSn").val(),    //标识
                                },
                                success: function (res) {
                                    if (res.code === 200) {
                                        layer.close(index);
                                        console.log(index);
                                        layer.msg("添加成功", {
                                            icon: 1
                                        }, function () {
                                            window.location.reload();
                                        });
                                    } else {
                                        layer.msg("添加失败", {
                                            icon: 5
                                        },function () {
                                            layer.close(index);
                                        });
                                    }
                                }
                            });
                        },
                        btn2: function (index, layero) {
                            $("#editRole").attr("style", "display:none;");
                        }
                        , end: function () {
                            $("#editName").val('');
                            $("#editSn").val('');
                        }
                    });
                    break;
            };
        });

        //监听行工具事件
        table.on('tool(article_bar)', function(obj){ //注：tool 是工具条事件名，test 是 table 原始容器的属性 lay-filter="对应的值"
            var data = obj.data //获得当前行数据
                ,layEvent = obj.event; //获得 lay-event 对应的值
            if(layEvent === 'detail'){
                layer.msg('查看操作');
            } else if(layEvent === 'del'){
                layer.confirm('确认删除角色吗?', function(index){
                    //向服务端发送删除指令
                    deleteUser(obj);
                    table.reload();
                });
            } else if(layEvent === 'edit'){
                layer.open({
                    type: 2, //type:0 也行
                    title: "授权",
                    area: ["400px", "82%"],
                    content: '/per/perDiaLog?roleId='+data.id,
                    btn: ['确定','取消']
                    ,yes: function(index, layero){
                        //获取iframe的body元素
                        var body = layer.getChildFrame('body',index);
                        //得到iframe页的窗口对象
                        var iframeWin = window[layero.find('iframe')[0]['name']];
                        //执行iframe页的showMsg方法
                        var result = iframeWin.get();
                        if(result !== -1){
                            layer.close(index);
                        }
                    }
                });
            }
        });
        function deleteUser(obj)
        {
            $.ajax({
                url:"/role/del?id="+obj.data.id,
                type: "get",
                dataType: "json",
                success: function (res) {
                    if (res.code === 200) {
                        layer.msg("删除成功", {
                            icon: 1
                        },function () {
                            obj.del();
                        });

                    } else {
                        layer.msg("删除失败,可能有拥有用户当前角色", {
                            icon: 5
                        });
                    }
                    layer.close(index);
                    console.log(index);

                }

            })
        }

    });
</script>
